package com.gxuwz.business.action;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;

import com.gxuwz.business.entity.Student;
import com.gxuwz.business.service.IStudentService;
import com.gxuwz.util.Pager;
import com.gxuwz.util.TimeSplitUtils;
import com.gxuwz.util.UidUtils;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

import net.sf.json.JSONObject;

public class StudentAction extends BaseAction implements Preparable,ModelDriven<Student>{

	private static final long serialVersionUID = -1598430537989849019L;
	
	private static final String STUDENT_LIST = "/WEB-INF/page/Student/student_list.jsp";
	private static final String STUDENT_ADD = "/WEB-INF/page/Student/student_add.jsp";
	private static final String STUDENT_UPDATE = "/WEB-INF/page/Student/student_update.jsp";
	
	
	@Autowired
	private IStudentService studentService;
	
	
	private Student student;//模型驱动
	private Pager<Student> studentList;//挂号分页数据
	private String errorMessage; //错误信息

	/**
	 * 打开挂号界面
	 */
	public String studentList(){
		forwardView = STUDENT_LIST;
		return SUCCESS;
	}

	/**
	 * 分页查询挂号信息
	 */
	public String getByPager(){
		//时间段查询
		if(null != timeSection && !"".equals(timeSection)){
			dateStringList = TimeSplitUtils.timeSplitToString(timeSection);
			String openTime = dateStringList.get(0);
			String endTime = dateStringList.get(1);
			student.setOpenTime(openTime);
			student.setEndTime(endTime);
//			System.out.println(student.getOpenTime()+" "+student.getEndTime());
		}
		studentList = studentService.getByPager(student, page, limit);
		JsonData = JSONObject.fromObject(studentList);
		return "JsonObject";
	}
	
	/**
	 * 打开添加挂号界面
	 */
	public String openAdd(){
		forwardView = STUDENT_ADD;
		return SUCCESS;
	}
	
	/**
	 * 添加学生挂号信息
	 */
	@SuppressWarnings("deprecation")
	public String add(){
		//随机生成挂号编号
		student.setRegId(UidUtils.UIDD());
		switch(student.getRegType()){
		case "外科":
			student.setRegPrice(3);break;
		case "内科":
			student.setRegPrice(5);break;
		}
		student.setRegDate(new Date());
		switch(student.getCareType()){
		case "农村合作医疗":
			student.setCareId("22222");break;
		case "城镇医疗":
			student.setCareId("11111");break;
		}
		student.setState(0);
		student.setStatus(0);
		isSucceed = studentService.add(student);
		jsonString = jsonToString(isSucceed);
		return "JsonString";
	}
	
	/**
	 * 检测学号是否已存在
	 */
	@SuppressWarnings("deprecation")
	public String checkStuId(){
		isSucceed =  studentService.checkStuId(student);
		jsonString = jsonToString(isSucceed);
		return "JsonString";
	}
	
	/**
	 * 伪删除挂号信息
	 */
	@SuppressWarnings("deprecation")
	public String delete(){
//		System.out.println(student);
		isSucceed = studentService.delete(student);
//		System.out.println(isSucceed);
		jsonString = jsonToString(isSucceed);
		return "JsonString";
		
	}
	
	/**
	 * 打开更新界面
	 */
	public String openUpdate(){
		student = studentService.findById(student);
		forwardView = STUDENT_UPDATE;
		return SUCCESS;
	}
	
	/**
	 * 更新信息
	 */
	@SuppressWarnings("deprecation")
	public String update(){
		System.out.println(student);
		isSucceed = studentService.update(student);
		jsonString = jsonToString(isSucceed);
		return "JsonString";
		
	}
	
	@Override
	public Student getModel() {
		return student;
	}

	@Override
	public void prepare() throws Exception {
		if(null == student){
			student = new Student();
		}
	}

	public String getErrorMessage() {
		return errorMessage;
	}

	public void setErrorMessage(String errorMessage) {
		this.errorMessage = errorMessage;
	}

	public Student getStudent() {
		return student;
	}

	public void setStudent(Student student) {
		this.student = student;
	}

	

}
